package com.turing.selfwebservice.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@SuppressWarnings("unchecked")
public class Helper {
	
	public static String JSON_Bean2String(Object ob, String[] filterFields) {
        JsonConfig j = new JsonConfig();
        j.registerJsonValueProcessor(String.class, new JsonValueProcessorImpl());
        j.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
        j.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());
        j.setExcludes(filterFields);
        JSONObject jsonObj = JSONObject.fromObject(ob, j);
        return jsonObj.toString();
    }

    public static String JSON_Bean2String(Object ob) {
		JsonConfig j = new JsonConfig();
		j.registerJsonValueProcessor(String.class, new JsonValueProcessorImpl());
		j.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
		j.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());
		JSONObject jsonObj = JSONObject.fromObject(ob, j);
		return jsonObj.toString();
    }

    public static String JSON_List2String(Object ob) {
        JsonConfig j = new JsonConfig();
        j.registerJsonValueProcessor(String.class, new JsonValueProcessorImpl());
        j.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
        j.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());
        JSONArray jsonArray = JSONArray.fromObject(ob, j);
        return jsonArray.toString();
    }

    public static String JSON_List2String(Object ob, String[] filterFields) {
        JsonConfig j = new JsonConfig();
        j.registerJsonValueProcessor(String.class, new JsonValueProcessorImpl());
        j.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
        j.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());
        j.setExcludes(filterFields);
        JSONArray jsonArray = JSONArray.fromObject(ob, j);
        return jsonArray.toString();
    }

    public static Object JSON_String2Bean(String s, Class bean) {
        JSONObject jsonObj = JSONObject.fromObject(s);
        return jsonObj.toBean(jsonObj, bean);
    }

    public static List JSON_String2List(String s, Class bean) {
        JSONArray jsonArray = JSONArray.fromObject(s);
        List list = (List) jsonArray.toCollection(jsonArray, bean);
        return list;
    }
    
    public static int getMaxValue(int[] maxInt) {
        int max;
        if (maxInt.length <= 0) {
            max = 0;
        }
        else {
            max = maxInt[0];
            for (int i = 0; i < maxInt.length; i++) {
                if (max < maxInt[i])
                    max = maxInt[i];
            }
        }
        return max;
    }

    public static int getMinValue(int[] maxInt) {
        int min;
        if (maxInt.length <= 0) {
            min = 0;
        }
        else {
            min = maxInt[0];
            for (int i = 0; i < maxInt.length; i++) {
                if (min > maxInt[i])
                    min = maxInt[i];
            }
        }
        return min;
    }

    public static int getMinValueIncludeZeor(int[] maxInt) {
        int min;
        if (maxInt.length <= 0) {
            return 0;
        }
        else {
            List ls = new ArrayList();
            for (int i = 0; i < maxInt.length; i++) {
                if (maxInt[i] == 0) {
                    continue;
                }
                ls.add(maxInt[i]);
            }
            if (ls.size() == 0) {
                return 0;
            }
            Collections.sort(ls);
            min = (Integer) ls.get(0);
            return min;
        }
    }
  
}
